// -----------------------------------------------
// 说明: 该文件中函数主要用于判断各个对象中返回值的合法性
// -----------------------------------------------

// 根据给定的对象和语句来得到返回值, 并作出相应提示
function CheckValid(exp1, exp2, fCtrlName, fAlertStr, retValue) {
	var fCtrlobj = document.all(fCtrlName);
	var expression = "";
	if (fCtrlobj == null) return !retValue;
	if (!(fCtrlobj.length > 1)) {
		expression = exp1 + ReplaceValue(fCtrlobj.value) + exp2;
		if (eval(expression)) {
			alert(fAlertStr);
			fCtrlobj.select();
			return retValue;
		}
	} else {
		var i;
		for(i=0;i<fCtrlobj.length;i++) {
			expression = exp1 + ReplaceValue(fCtrlobj[i].value) + exp2;
			if(eval(expression)) {
				alert(fAlertStr);
				fCtrlobj[i].select();
				return retValue;
			}
		}
	}
	return !retValue;
}

function ReplaceValue(strValue)
{
   var str = strValue;
   str = str.replace(/\r/g, "");
   str = str.replace(/\n/g, "");
   str = str.replace(/\\/g, "\\\\");
   str = str.replace(/'/g, "\\\'");
   return str;
}

// 检查是否为空, 是则提醒
function IsEmpty(fData) {
    return ((fData==null) || (fData.length==0));
}
function CheckEmpty(fCtrlName, fAlertStr) {
	return CheckValid("IsEmpty('", "')", fCtrlName, fAlertStr, false);
}

// 检查是否为数字, 否则提醒
function IsDigit(fData) {
	return ((fData>="0") && (fData<="9"));
}
function CheckDigit(fCtrlName, fAlertStr) {
	return CheckValid("!IsDigit('", "')", fCtrlName, fAlertStr, false);
}

// 检查字符串长度是否超过指定长度, 是则提醒
function DataLength(fData) {
    var intLength = 0;
    for (var i=0; i<fData.length; i++) {
		if ((fData.charCodeAt(i) < 0) || (fData.charCodeAt(i) > 255))
			intLength += 2;
        else
            intLength ++;
    }
    return intLength;
}
function CheckDataLength(fCtrlName, fAlertStr, MaxLength) {
	return CheckValid("DataLength('", "')>"+MaxLength, fCtrlName, fAlertStr, false);
}

function CheckFixLength(fCtrlName, fAlertStr, MaxLength) {
	return CheckValid("DataLength('", "')!="+MaxLength, fCtrlName, fAlertStr, false);
}

// 检查是否为整型数字, 否则提醒
function IsInteger(fData) {
	if (IsEmpty(fData))
		return true;
	if ((isNaN(fData)) || (fData.indexOf(".")!=-1) || (fData.indexOf("-")!=-1))
		return false;
	return true;
}
function CheckInteger(fCtrlName, fAlertStr) {
	return CheckValid("!IsInteger('", "')", fCtrlName, fAlertStr, false);
}

// 检查是否为合法Email, 否则提醒
function IsEmail(fData) {
	if (IsEmpty(fData))
		return true;
	if (fData.indexOf("@")==-1)
		return false;
	return true;
}
function CheckEmail(fCtrlName, fAlertStr) {
	return CheckValid("!IsEmail('", "')", fCtrlName, fAlertStr, false);
}

// 检查是否为正值, 否则提醒
function IsPlusNumeric(fData) {
	if (IsEmpty(fData))
		return true;
	if ((isNaN(fData)) || (fData.indexOf("-")!=-1))
		return false;
	return true;
}
function CheckPlusNumeric(fCtrlName, fAlertStr) {
	return CheckValid("!IsPlusNumeric('", "')", fCtrlName, fAlertStr, false);
}

// 检查是否为数值, 否则提醒
function IsNumeric(fData) {
	if (IsEmpty(fData))
		return true;
	if (isNaN(fData))
		return false;
	return true;
}
function CheckNumeric(fCtrlName, fAlertStr) {
	return CheckValid("!IsNumeric('", "')", fCtrlName, fAlertStr, false);
}

// 检查是否为指定范围内的数值, 否则提醒
function IsNumericInRange(fInput,fLower,fHigh) {
	if (!IsNumeric(fInput))
		return false;
	if (fLower == null)
		return (fInput <= fHigh);
	else if (fHigh == null)
		return (fInput >= fLower);
	else
		return ((fInput >= fLower) && (fInput <= fHigh));
}
function CheckNumericInRange(fCtrlName, fAlertStr, fLower, fHigh){
	return CheckValid("!IsNumericInRange(", ","+fLower+","+fHigh+")", fCtrlName, fAlertStr, false);
}

// 检查是否为有效日期, 否则提醒
function IsDate(fDate) {
    if (IsEmpty(fDate))
        return true;
	if (vbIsDate(fDate))
		return true;
	return false;
}
function CheckDate(fCtrlName, fAlertStr) {
	return CheckValid("!IsDate('", "')", fCtrlName, fAlertStr, false);
}